<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) https://inphp.cc All rights reserved.
// | 该文件源码由INPHP官方提供，使用协议以INPHP官方公告为准。
// +----------------------------------------------------------------------
// | 内容详情页面
// +----------------------------------------------------------------------
namespace app\cms\http\web;

use app\cms\model\ArchiveModel;
use app\cms\tags\next;
use app\cms\tags\prev;
use Inphp\Core\Context;
use Inphp\Core\Db\Redis;

class archive extends controller
{
    /**
     * 文章详情页
     * @param string|null $id
     */
    public function index(?string $id)
    {
        //设置当前展示页面类型
        Context::set("CMS_PAGE", "archive");
        //自动识别ID
        $id = $id ?? GET("id");
        $archive = \app\cms\tags\archive::process([
            "id"    => $id
        ]);
        if (empty($archive)) {
            $this->show404();
            return;
        }
        //获取上一篇
        $prev = prev::process([
            "id"        => $archive["id"],
            //"categoryId"=> $archive["categoryId"],
            "assign"    => true
        ]);
        $archive["prev"] = !empty($prev) ? $prev : null;
        //获取下一篇
        $next = next::process([
            "id"        => $archive["id"],
            //"categoryId"=> $archive["categoryId"],
            "assign"    => true
        ]);
        $archive["next"] = !empty($next) ? $next : null;
        viewAssign("CMS_PAGE_TITLE", $archive["title"]);
        if (!empty($archive["keyword"])) {
            viewAssign("CMS_PAGE_KEYWORD", $archive["keyword"]);
        }
        if (!empty($archive["description"])) {
            viewAssign("CMS_PAGE_DESCRIPTION", $archive["description"]);
        }
        //查询分类
        $category = [];
        if ($archive["categoryId"] > 0) {
            $category = \app\cms\tags\category::process([
                "id"       => $archive["categoryId"]
            ]);
            if (!empty($category)) {
                viewAssign("category", $category);
            }
        }
        //处理模板
        $tpl = !empty($archive["template"]) ? $archive["template"] : (!empty($category["templateArchive"]) ? $category["templateArchive"] : null);
        //设置模板
        $this->setTPL($tpl);
        //记录文章访问量
        //获取点击量
        $clicks = Redis::get("CMS_ARCHIVE_CLICKS_{$archive["id"]}");
        $clicks = !empty($clicks) && is_numeric($clicks) ? ceil($clicks) : $archive["clicks"];
        //以IP为KEY，保存阅读，30分钟可刷新
        $key = md5(getIP()."-".$archive["id"]."-click");
        $check = Redis::get($key);
        if (empty($check)) {
            @ArchiveModel::emptyQuery()
                ->where("id", $archive["id"])
                ->increment("clicks")
                ->update();
            Redis::set($key, "click", 30 * 60);
            //自增
            Redis::incr("CMS_ARCHIVE_CLICKS_{$archive["id"]}");
            $clicks++;
        }
        $archive["clicks"] = $clicks;
        viewAssign("archive", $archive);
        //兼容变量名
        viewAssign("article", $archive);
    }

    /**
     * 兼容地址识别
     * @param string $name
     * @param array $arguments
     */
    public function __call(string $name, array $arguments)
    {
        // TODO: Implement __call() method.
        $name = strrchr($name, ".html") === ".html" ? substr($name, 0, -5) : $name;
        $this->index($name);
    }
}